<?php

/**
 * MinimunCMS
 *
 * Modulo de Internos
 * Muestra listado de internos
 *
 * @link http://www.minimuncms.com.ar
 * @copyright 2010 Sergio Sambataro
 * @author Sergio Sambataro <scs.1975@gmail.com>
 * @package minimuncms
 * @version 2.0
*/

class Internos_empresa2 extends Controller {

    private $tabla = "min_internos_empresa2";

    function Internos_empresa2() {
        parent::Controller();

        $this->load->model('admin/modelo_modulos');

        if(!$this->session->userdata('logged_in') || $this->modelo_modulos->get_permisos('internos_empresa2_principal','empresa2')=='no')
            redirect('/home/');

        $this->load->library('session');
        $this->load->language('general',$this->session->userdata('lenguaje'));
        $this->load->language('internos_empresa2',$this->session->userdata('lenguaje'));
    }

    function index() {
        $datos['titulo_pagina'] = 'Internos';
        $this->modelo_modulos->get_modulos();

        $this->load->view('encabezado', $datos);
        $this->load->view('menu');
        $this->load->view('internos_empresa2');
        $this->load->view('pie');
    }

    /**
     * Genera contenido para la tabla con Json
     */
    function post() {

        $page         = $this->input->post('page');
        $limit        = $this->input->post('rows');
        $sidx         = $this->input->post('sidx');
        $sord         = $this->input->post('sord');
        $_search      = $this->input->post('_search');
        $searchField  = $this->input->post('searchField');
        $searchString = $this->input->post('searchString');

        $where = "";
        if ($_search=='true')
            $where = " WHERE $searchField LIKE '%$searchString%' ";

        if(!$sidx) $sidx =1;

        $sql    = "SELECT * FROM $this->tabla $where";
        $query  = $this->db->query($sql);
        $result = $query->result();

        $count  = count($result);

        if( $count > 0 && $limit > 0) {
            $total_pages = ceil($count/$limit);
        } else {
            $total_pages = 0;
        }

        if ($page > $total_pages) $page=$total_pages;
        $start = $limit*$page - $limit;
        if($start <0) $start = 0;

        $sql    = "SELECT * FROM $this->tabla $where ORDER BY $sidx $sord LIMIT $start , $limit";
        $query  = $this->db->query($sql);

        header("Expires: Mon, 26 Jul 1997 05:00:00 GMT" );
        header("Last-Modified: " . gmdate( "D, d M Y H:i:s" ) . "GMT" );
        header("Cache-Control: no-cache, must-revalidate" );
        header("Pragma: no-cache" );
        header("Content-type: text/x-json");

        $i=0;
        $data['page']= $page;
        $data['total']= $total_pages;

        foreach ($query->result() as $row) {
            $data['rows'][$i]['id']= $row->id;
            $data['rows'][$i]['cell'][]= $row->id;
            $data['rows'][$i]['cell'][]= utf8_encode($row->legajo);
            $data['rows'][$i]['cell'][]= $row->nombre;
            $data['rows'][$i]['cell'][]= utf8_encode($row->interno);
            $data['rows'][$i]['cell'][]= utf8_encode($row->email1);
            $data['rows'][$i]['cell'][]= "<a href='".base_url()."index.php/internos/modificar/$row->id'>Modificar</>";
            $i++;
        }
        echo json_encode($data);

    }

    /**
     * ABM
     */
    function guardar() {

        $oper    = $this->input->post('oper');
        $id      = $this->input->post('id');
        $legajo  = $this->input->post('legajo');
        $nombre  = $this->input->post('nombre');
        $interno = $this->input->post('interno');
        $email1  = $this->input->post('email1');

        if($oper=='edit') {
            $query = $this->db->query("UPDATE $this->tabla set legajo='".$legajo."', nombre='".$nombre."', interno='".$interno."', email1='".$email1."' WHERE id=".$id);
        }elseif($oper=='add') {
            $query = $this->db->query("INSERT INTO $this->tabla set legajo='".$legajo."', nombre='".$nombre."', interno='".$interno."', email1='".$email1."'");
        }elseif($oper=='del') {
            $query = $this->db->query("DELETE FROM $this->tabla WHERE id=$id");
        }

    }

}
?>